package com.win.controller.sqoop;

import java.util.Scanner;

import com.jcraft.jsch.JSchException;
import com.win.utils.Result;
import org.springframework.web.bind.annotation.*;


import java.io.IOException;
import java.util.Scanner;

@RestController
@RequestMapping("/sqoop")
@CrossOrigin
public class ExampleUsage {

    @GetMapping
    public Result sqoop(@RequestParam("database") String database ,@RequestParam("table") String table) throws JSchException, IOException {

        SqoopCommand command = new SqoopCommand(new String[]{
                "sqoop", "import",
                "--connect", "jdbc:mysql://192.168.30.130:3306/"+database,
                "--username", "root",
                "--password", "Wx041800@",
                "--table",table,
                "--split-by","id",
                "--target-dir", "/project",
                "--fields-terminated-by", ",",
                "--lines-terminated-by", "\"\n\"",
                "-m","1",
                "--delete-target-dir"
        });

        SqoopExecutor executor = new SqoopExecutor("192.168.30.130", "root", "wx041800");

        if (executor.testConnection()) {
            System.out.println("SSH Connection Successful!");
        } else {
            System.out.println("SSH Connection Failed!");
        }
        System.out.println(command);
        SqoopResult result = executor.execute(command);

        System.out.println("Output: " + result.getOutput());
        System.out.println("Error: " + result.getError());
        System.out.println("Exit status: " + result.getExitStatus());

        return Result.ok(null);
    }

}